import { NMenu, type MenuProps, type MenuInst } from "naive-ui";
import type { HTMLAttributes } from "vue";

export type MenuFactoryConstructorType = {
  props?: MenuProps;
  attrs?: HTMLAttributes;
};

export class MenuFactory {
  private attrs: HTMLAttributes = {};
  private props: MenuProps = {};
  private domRef: MenuInst | null = null;

  constructor(data?: MenuFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: MenuProps) {
    this.props = props;
    return this;
  }

  deriveResponsiveState() {
    this.domRef?.deriveResponsiveState();
  }

  showOption(key?: string | number) {
    this.domRef?.showOption(key);
  }

  create() {
    return (
      <NMenu
        ref={(el: HTMLElement) => {
          this.domRef = el as unknown as MenuInst;
        }}
        {...this.props}
        {...this.attrs}
      ></NMenu>
    );
  }
}
